home *** CD-ROM | disk | FTP | other *** search
/ ...taking it to the Macs! / ...taking it to the Macs!.iso / Extras / ActiveX Mac SDK / ActiveX SDK / Sample Controls / Button2 / CButton2Control.cp < prev    next >
Encoding:
Text File  |  1996-12-21  |  6.5 KB  |  269 lines  |  [TEXT/CWIE]

  1. #include "ocheaders.h"
  2. #include "BDDISPIDs.h"
  3. #include "CButton2Control.h"
  4. #include "BDConsts.h"
  5. #include "BDUtils.h"
  6. #include "FnAssert.h"
  7. #include "dispatch.h"
  8. #include <LArray.h>
  9. #include "CConnectionPoint.h"
  10. #include "CCPContainer.h"
  11. //#include "CButtonError.h"
  12. #include <iterator.h>
  13. #include <ctype.h>
  14. #include <math.h>
  15. #include <stdio.h>
  16.  
  17. ///////////////////////////////////////////////////////////////////////////////
  18. //
  19. //  CButtonControl::CButtonControl
  20. //
  21.  
  22. CButtonControl::CButtonControl(void)
  23. {
  24.     mButtonControl         = NULL;
  25.     mDoTracking         = false;
  26.  
  27.     mCaption = NULL;
  28.     mID[0] = 0;
  29.     
  30.     // CBaseEventServer setup
  31.     AddOutGoingInterface(IID_IDoMenuEvents);
  32.     SetUpConnectionPoints();
  33. }
  34.  
  35. ///////////////////////////////////////////////////////////////////////////////
  36. //
  37. //  CButtonControl::~CButtonControl
  38. //
  39.  
  40. CButtonControl::~CButtonControl(void)
  41. {
  42.     if ( mCaption )
  43.     {
  44.         delete [] mCaption;
  45.         mCaption = NULL;
  46.     }
  47. }
  48.  
  49. ///////////////////////////////////////////////////////////////////////////////
  50. //
  51. // CButtonControl::IUnknown::QueryInterface
  52. //
  53. //  Returns a pointer to the specified interface on a component to which a
  54. //  client currently holds an interface pointer.
  55. //
  56.  
  57. STDMETHODIMP
  58. CButtonControl::QueryInterface(REFIID refID, void** obj)
  59. {
  60.     HRESULT result = E_NOINTERFACE;
  61.     
  62.     result = CBaseEventServer::QueryInterface(refID, obj);
  63.     if ( result == S_OK )
  64.         goto labelExit;
  65.  
  66.     result = CBaseControl::QueryInterface(refID, obj);
  67.     if ( result == S_OK )
  68.         goto labelExit;
  69.  
  70. labelExit:
  71.     return result;
  72. /*
  73. OBSOLETE!
  74.  
  75.     if ( RefID == IID_IDoMenuEvents ) // an outgoing interface
  76.     {
  77.         *Obj = (void*) (IDoMenuEvents*) this;
  78.         AddRef();
  79.         
  80.         return ResultFromScode(S_OK);
  81.     }
  82.     else
  83.         return CBaseControl::QueryInterface(RefID, Obj);
  84. */
  85. }
  86.  
  87. ///////////////////////////////////////////////////////////////////////////////
  88. //
  89. //  CButtonControl::IInProcObject::Draw
  90. //
  91.  
  92. STDMETHODIMP
  93. CButtonControl::Draw(THIS_ DrawContext* Context)
  94. {
  95.     if (Context->DrawAspect != DVASPECT_CONTENT)
  96.         return ResultFromScode(DV_E_DVASPECT);
  97.         
  98.     if ( mButtonControl )
  99.          UpdateButtonControl();
  100.     else
  101.         CreateButtonControl(((WindowPtr)Context->Port), &(Context->Location));
  102.     
  103.     return ResultFromScode(S_OK);
  104. }
  105.  
  106. ///////////////////////////////////////////////////////////////////////////////
  107. //
  108. //  CButtonControl::IControl::DoMouse
  109. //
  110.  
  111. STDMETHODIMP
  112. CButtonControl::DoMouse(THIS_ MouseEventType inMouseET, PlatformEvent* inEvent)
  113. {
  114.     switch (inMouseET) 
  115.     {
  116.         case MouseDown:
  117.         {
  118.             if ( mDoTracking )
  119.             {
  120.                 // NOTE: any handlers won't actually get the mouse down event
  121.                 // until after we're done tracking, because it's asychronous.
  122.                 FireEvent(IID_IStandardEvents, DISPID_MOUSEDOWN, inEvent);
  123.                 if ( Track(inEvent) == inButton )
  124.                     FireEvent(IID_IStandardEvents, DISPID_MOUSEUP, inEvent);
  125.             }
  126.             else
  127.                 FireEvent(IID_IDoMenuEvents, DISPID_POPUP, inEvent);
  128.                 
  129.             break;
  130.         }
  131.  
  132.         case MouseUp:
  133.             // in case we're supporting mouse tracking, don't do anything for
  134.             // a mouse up event, since we handle it with the mouse down.
  135.             if ( mDoTracking )
  136.                 break;
  137.             else // of course, we don't do anything otherwise right now, either.
  138.                 break;
  139.  
  140.     }
  141.  
  142.     return ResultFromScode(S_OK);
  143. }
  144.  
  145. ///////////////////////////////////////////////////////////////////////////////
  146. //
  147. //  CButtonControl::IPersistPropertyBag::Load
  148. //
  149.  
  150. STDMETHODIMP
  151. CButtonControl::Load(IPropertyBag* propertyBag, IErrorLog* errorLog)
  152. {
  153.     char        propertyString[Str255BufferLength];
  154.  
  155.     // try to load in each property.  if we can't get it, then leave
  156.     // things at the default.
  157.     
  158.     // The name of the button
  159.     if ( ::LoadPropertyString(propertyBag, "caption", propertyString, Str255StringLength, errorLog) )
  160.     {
  161.         if ( mCaption )
  162.         {
  163.             delete [] mCaption;
  164.             mCaption = NULL;
  165.         }
  166.         
  167.         mCaption = (char *) new char[Str255BufferLength];
  168.         strcpy(mCaption, propertyString);
  169.     }
  170.  
  171.     // do we want to track the mouse in the standard Mac way?
  172.     if ( ::LoadPropertyString(propertyBag, "tracking", propertyString, Str255BufferLength, errorLog) )
  173.     {
  174.         if ( !strcmp(propertyString, "1") )
  175.             mDoTracking = true;
  176.     }
  177.     
  178.     // Eventually we may be able to inherit this from CBaseControl, or
  179.     // an as-yet undefined base class that simply loads the ID paramter.
  180.     // When the framework supports GetID instead of GetName in IControl, then
  181.     // mID may change to something else, as well.
  182.     if ( ::LoadPropertyString(propertyBag, "sourceid", propertyString, Str255StringLength, errorLog) )
  183.     {
  184.         strcpy((char*)(&mID[1]), propertyString);
  185.         mID[0] = strlen(propertyString);
  186.     }
  187.  
  188.     return ResultFromScode(S_OK);
  189. }
  190.  
  191. ///////////////////////////////////////////////////////////////////////////////
  192. //
  193. //  CEventSender::FireOneEvent
  194. //
  195.  
  196. STDMETHODIMP
  197. CButtonControl::FireOneEvent(REFIID RefID, long EventID, IUnknown* EventTarget, PlatformEvent* Event)
  198. {
  199.     IDoMenuEvents*        popTarget = (IDoMenuEvents*) EventTarget;
  200.     IUnknown*                    unk;
  201.     
  202.     this->QueryInterface(IID_IUnknown, (void**) &unk);
  203.     
  204.     switch ( EventID )
  205.     {
  206.         case DISPID_POPUP:
  207.             popTarget->Popup(unk, Event);
  208.             break;
  209.     }
  210.     
  211.     return ResultFromScode(S_OK);
  212. }
  213.  
  214. ///////////////////////////////////////////////////////////////////////////////
  215. //
  216. //  CButtonControl::CreateButtonControl
  217. //
  218. void CButtonControl::CreateButtonControl(const WindowPtr window, const Rect * ctrlRect)
  219. {
  220.     c2pstr(mCaption);
  221.  
  222.     mButtonControl = ::NewControl (    window,                 // the grafport
  223.                                     ctrlRect,                 // where to draw it
  224.                                     (StringPtr)mCaption,     // button name/title
  225.                                     true,                    // it's visible
  226.                                     0,                        // initial value - ignored
  227.                                     0,                        // min value - ignored
  228.                                     0,                        // max value - ignored
  229.                                     pushButProc,            // it's a button
  230.                                     0);                        // user-defined refcon -- not used
  231.     ASSERT(mButtonControl != NULL, "NULL Control");
  232.  
  233.     p2cstr((StringPtr)mCaption);
  234. }
  235.  
  236. ///////////////////////////////////////////////////////////////////////////////
  237. //
  238. //  CButtonControl::Track
  239. //
  240.  
  241. short CButtonControl::Track(PlatformEvent* Event)
  242. {
  243.     short releasePoint = 0;
  244.     
  245.     ASSERT(Event != NULL, "NULL event pointer!");        
  246.  
  247.     // We need to Acquire here, so that TrackControl is focused
  248.     DrawContext    Context = {(PortType) 0};
  249.     mContainerSiteP->AcquireContext(mActiveContext->GetContextID(), &Context);    
  250.  
  251.     if ( mButtonControl )
  252.         releasePoint = ::TrackControl(mButtonControl, Event->where, NULL);
  253.         
  254.     mContainerSiteP->ReleaseContext(&Context);
  255.  
  256.     return releasePoint;
  257. }
  258.  
  259. ///////////////////////////////////////////////////////////////////////////////
  260. //
  261. //  CButtonControl::UpdateButtonControl
  262. //
  263.  
  264. void CButtonControl::UpdateButtonControl(void)
  265. {
  266.     if ( mButtonControl )
  267.         ::Draw1Control(mButtonControl);
  268. }
  269.